package com.dragonfly.shutter.services;

import java.sql.Date;

import com.dragonfly.shutter.DAOs.BrowseLogDAO;
import com.dragonfly.shutter.DAOs.PicCommentDAO;
import com.dragonfly.shutter.beans.BrowseLog;
import com.dragonfly.shutter.beans.PagedBean;

/**
 * 评论列表服务类
 * @author 黄挺
 *
 */
public class CommentListService {
	private BrowseLogDAO browseLogDAO;
	private PicCommentDAO picCommentDAO;
	private static int pageSize = 10;
	
	/**
	 * 评论列表服务函数
	 * @param pictureID 图片ID
	 * @param userID 用户ID
	 * @param page 分页页码
	 * @param requestURL 请求URL
	 * @return 分页Bean
	 */
	public PagedBean doService(String pictureID, Integer userID, Integer page,
			String requestURL){
		BrowseLog browseLog = null;
		
		if(userID == null )
			browseLog = new BrowseLog(new Date(System.currentTimeMillis()), requestURL);
		else
			browseLog = new BrowseLog(userID, new Date(System.currentTimeMillis()), requestURL);
		
		PagedBean pagedBean = new PagedBean();
		pagedBean.setPageNum(page);
		pagedBean.setPageSize(pageSize);
		
		return doDBOperate(pictureID, page, browseLog, pagedBean);
	}
	
	/**
	 * 评论列表数据库操作函数
	 * @param pictureID 图片ID
	 * @param page 分页页码
	 * @param browseLog 日志Bean
	 * @param pagedBean 分页Bean
	 * @return
	 */
	private PagedBean doDBOperate(String pictureID, Integer page,
			BrowseLog browseLog, PagedBean pagedBean) {
		int minRow = ( page - 1 ) * pageSize + 1;
		int maxRow = page * pageSize; 
		
		pagedBean.setPagedItem(picCommentDAO.selectPagedCommentByPicID(pictureID, minRow, maxRow));
		pagedBean.setItemNum(picCommentDAO.selectCommentCountByPicId(pictureID));
		int maxPage = (int) java.lang.Math.ceil( (double)pagedBean.getItemNum() / (double)pageSize );
		pagedBean.setMaxPage(maxPage);
		browseLogDAO.insertBrowseLog(browseLog);
		
		return pagedBean;
	}

	public void setBrowseLogDAO(BrowseLogDAO browseLogDAO) {
		this.browseLogDAO = browseLogDAO;
	}

	public void setPicCommentDAO(PicCommentDAO picCommentDAO) {
		this.picCommentDAO = picCommentDAO;
	}

}
